package com.bckj.fastboot.core.sql;

import com.bckj.fastboot.core.sql.meta.Column;

import java.util.Collection;
import java.util.function.Consumer;

public interface SqlConditionBuilder<Children extends SqlConditionBuilder<Children>> {

    Children getChildren();

    Children where(String field, Object value, String opt);

    default Children eq(boolean condition, Column column, Object value) {
        if (condition) {
            eq(column, value);
        }
        return getChildren();
    }

    default Children eq(Column column, Object value) {
        return eq(column.getTableColumnName(), value);
    }

    Children eq(String field, Object value);

    default Children ne(boolean condition, Column column, Object value) {
        if (condition) {
            ne(column, value);
        }
        return getChildren();
    }

    default Children ne(Column column, Object value) {
        return ne(column.getTableColumnName(), value);
    }

    Children ne(String field, Object value);

    default Children gt(boolean condition, Column column, Object value) {
        if (condition) {
            gt(column, value);
        }
        return getChildren();
    }

    default Children gt(Column column, Object value) {
        return gt(column.getTableColumnName(), value);
    }

    Children gt(String field, Object value);
    default Children ge(boolean condition, Column column, Object value) {
        if (condition) {
            ge(column, value);
        }
        return getChildren();
    }

    default Children ge(Column column, Object value) {
        return ge(column.getTableColumnName(), value);
    }

    Children ge(String field, Object value);

    default Children lt(boolean condition, Column column, Object value) {
        if (condition) {
            lt(column, value);
        }
        return getChildren();
    }

    default Children lt(Column column, Object value) {
        return lt(column.getTableColumnName(), value);
    }

    Children lt(String field, Object value);

    default Children le(boolean condition, Column column, Object value) {
        if (condition) {
            le(column, value);
        }
        return getChildren();
    }

    default Children le(Column column, Object value) {
        return le(column.getTableColumnName(), value);
    }

    Children le(String field, Object value);

    default Children like(boolean condition, Column column, Object value) {
        if (condition) {
            like(column, value);
        }
        return getChildren();
    }

    default Children like(Column column, Object value) {
        return like(column.getTableColumnName(), value);
    }

    Children like(String field, Object value);

    default Children likeLeft(boolean condition, Column column, Object value) {
        if (condition) {
            return likeLeft(column, value);
        }
        return getChildren();
    }

    default Children likeLeft(Column column, Object value) {
        return likeLeft(column.getTableColumnName(), value);
    }

    Children likeLeft(String field, Object value);

    default Children likeRight(boolean condition, Column column, Object value) {
        if (condition) {
            return likeRight(column, value);
        }
        return getChildren();
    }

    default Children likeRight(Column column, Object value) {
        return likeRight(column.getTableColumnName(), value);
    }

    Children likeRight(String field, Object value);

    default Children between(boolean condition, Column column, Object val1, Object val2) {
        if (condition) {
            return between(column, val1, val2);
        }
        return getChildren();
    }

    default Children between(Column column, Object val1, Object val2) {
        return between(column.getTableColumnName(), val1, val2);
    }

    Children between(String field, Object val1, Object val2);

    default Children in(boolean condition, Column column, Collection<?> coll) {
        if (condition) {
            return in(column, coll);
        }
        return getChildren();
    }

    default Children in(Column column, Collection<?> coll) {
        return in(column.getTableColumnName(), coll);
    }

    Children in(String field, Collection<?> coll);

    default Children notIn(boolean condition, Column column, Collection<?> coll) {
        if (condition) {
            return notIn(column, coll);
        }
        return getChildren();
    }

    default Children notIn(Column column, Collection<?> coll) {
        return notIn(column.getTableColumnName(), coll);
    }

    Children notIn(String field, Collection<?> coll);

    default Children isNull(Column... columns) {
        return getChildren();
    }

    default Children isNull(boolean condition, String column) {
        if (condition) {
            return isNull(column);
        }
        return getChildren();
    }

    Children isNull(String column);

    default Children isNotNull(Column... columns) {
        return getChildren();
    }

    default Children isNotNull(boolean condition, String column) {
        if (condition) {
            return isNotNull(column);
        }
        return getChildren();
    }

    Children isNotNull(String column);

    Children and(Consumer<SqlConditionBuilder<?>> consumer);

    default Children or() {
        return getChildren();
    }
}
